package com.whately.core.base.utils;

import lombok.Getter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import javax.annotation.PostConstruct;
import java.io.File;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 获取jar所在的路径
 * 如果不是jar包，则返回targetluj
 * //构建
 * String relativeDir=jarDirectoryUtil.getJarDirectory() + File.separator +"signcfg"+File.separator ;
 *
 */
@Component
public class JarDirectoryUtil {
    private static Logger logger = LoggerFactory.getLogger(JarDirectoryUtil.class);
    private static AtomicBoolean inited =new AtomicBoolean(false);
    @Getter
    private  String jarDirectory ;

    @PostConstruct
    public void start() throws Exception{
        if(!inited.compareAndSet(false, true)){
            return;
        }

        String tmp = new File(ResourceUtils.getURL("classpath:").getPath()).getParentFile().getParentFile().getParent();

        if(tmp.startsWith("file:")){
            logger.info("jarDirectory start with file:,remove it");
            jarDirectory = tmp.substring(5);
        }else{
            jarDirectory = tmp;
        }
        logger.info("JarDirectoryUtil start jar directory is {}",jarDirectory);
        //D:\Users\Documents\a\b\target
        //System.out.println("jarDirectory is "+jarDirectory);

        /**
         *  File uploadFile = new File(fullPath + newFileName);
         *             if (!uploadFile.getParentFile().exists()) { //判断文件父目录是否存在
         *                 uploadFile.getParentFile().mkdirs();
         *             }
         */
    }


}
